<?php

class CpmagazineController extends Core_Controller_Action 
{
	private $_dimension;
	
	public function init() {
	
		$this->_dimension = new Dimension();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpmagazine' );
	}
	
	public function itemAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );

		$this->assignType( 0 );
	}

	public function listAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		

		$this->view->producent = $this->_request->getParam('producent', '');
		$this->view->kategoria = $this->_request->getParam('kategoria', '');
		$this->view->podkategoria = $this->_request->getParam('podkategoria', '');
		$this->view->tag = $this->_request->getParam('tag', '');
		$this->view->nazwa = $this->_request->getParam('nazwa', '');
		
		$tagi = new Tags();
		$tag = $tagi->getList();
		$list=array(0=>'');
		foreach($tag as $jeden)
			$list[$jeden['t_id']]=$jeden['name_pl'];  
		$this->view->tag_list=$list;
		$tab_in = array();
		
		if($this->view->tag!='0' AND $this->view->tag!='')
		{
			$tagi = new TagsHasProducts();
			$tag_in = $tagi->getListProd($this->view->tag);
			$tag_tab = array();
			foreach($tag_in as $tag)
				$tab_in[]=$tag['products_p_id'];
		}
		
		
		$tagi = new Categries();
		$tag = $tagi->getListAll();
		$list=array(0=>'');
		$list2=array(0=>'');
		foreach($tag as $jeden)
		{
			if($jeden['parent_id']==0)
				$list[$jeden['c_id']]=$jeden['name_pl'];
			else
				$list2[$jeden['c_id']]='	'.$jeden['name_pl'];
		}   
		$this->view->kat_list=$list;
		$this->view->podkat_list=$list2;
		
		if($this->view->kategoria!='0'  AND $this->view->kategoria!='')
		{
			$tagi = new CategriesHasProducts();
			$tag_in = $tagi->getListProd($this->view->kategoria);
			$kat_tab = array();
			foreach($tag_in as $tag)
				$tab_in[]=$tag['products_p_id'];
		}
		if($this->view->podkategoria!='0' AND $this->view->podkategoria!='')
		{
			$tagi = new CategriesHasProducts();
			$tag_in = $tagi->getListProd($this->view->podkategoria);
			$podkat_tab = array();
			foreach($tag_in as $tag)
				$tab_in[]=$tag['products_p_id'];
		}
		if(empty($tab_in))
			$tab_in = '';
		else
		{	
			$pomoc = '';
			$tab_in=array_unique($tab_in);
			foreach($tab_in as $one)
				$pomoc .= $one.',';
			$tab_in = substr($pomoc, 0 , -1); 
		}
		
		
		$man = new Manufacturers();
		$man_l = $man->getList();
		$list=array(0=>'');
		foreach($man_l as $jeden)
			$list[$jeden['m_id']]=$jeden['name_pl'];  
		$this->view->producent_list=$list;

		$pagerLayout = $this->_dimension->getListWithPager( $page, $this->view->baseUrl(), $this->view->producent, $this->view->nazwa, $tab_in, $this->view->kategoria, $this->view->podkategoria, $this->view->tag );

		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;

		$this->assignType( 0 );
	}
	
	public function generatecsvAction() {
		$this->_helper->layout->disableLayout();

		$this->view->list = $this->_dimension->getListForCsv();

		$this->getResponse()->setHeader( 'Content-type', 'application/txt' );
		$this->getResponse()->setHeader( 'Content-Disposition', 'attachment; filename="magazyn.csv"' );
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Vat';
				break;
		}
	}
}